<?php
if( ! defined('BASEPATH')) exit('No direct script access allowed');

class Worksheet extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		// check if user logged in
		if(!$this->ion_auth->logged_in()){
			redirect('auth/login');	}
		$this->load->model('founder/worksheet_model');		
		$this->load->library('form_validation');
	}
	
	function all_worksheet($year_id = null,$month_id = null,$date_id = null){		
		$meta['page_title'] = "All Worksheet";		
		$this->data['users'] = $this->ion_auth->users()->result();
		$this->data['year_id'] = $year_id;
		$this->data['month_id'] = $month_id;
		$this->data['date_id'] = $date_id;
		$this->data['months'] = $this->worksheet_model->get_months();
		//$this->data['worksheet'] = $this->worksheet_model->get_all_worksheet_by_date($where);
		
		$this->load->view('common/header', $meta);
		$this->load->view('founder/all-worksheet',$this->data);
		$this->load->view('common/footer');	
	}
	
	function add_remarks(){
				
		$where     = array(
			'year_id' =>$_REQUEST['year_id'],
			'month_id'=>$_REQUEST['month_id'],
			'user_id' =>$_REQUEST['user_id'],
			'date_id' =>$_REQUEST['date_id'],
			'remark'  =>$_REQUEST['remark']
		);
		
		$this->worksheet_model->add_worksheet_remark($where);
		redirect('founder/worksheet/all_worksheet','refresh');
	}
	
	function manage_hashtag(){
		
		$data['get_hashtags'] = $this->worksheet_model->get_all_hashtag();
		$meta['page_title'] = "Manage Hashtag";
		$this->load->view('common/header', $meta);
		$this->load->view('founder/all-hashtags',$data);
		$this->load->view('common/footer');	
	}
	
	function add_hashtags(){
		
		$insert=$this->input->post('hashtag');
		$this->worksheet_model->insert_hashtag($insert);
		redirect('founder/worksheet/manage_hashtag');
	}
	
	function delete_remark($id)
	{
		$this->worksheet_model->delete_remarks($id);
		redirect('founder/worksheet/all_worksheet','refresh');
	}
	
	function delete_hashtags($id)
	{
		$this->worksheet_model->delete_hashtag($id);
		redirect('founder/worksheet/manage_hashtag','refresh');
	}
	
	function add_colors(){
		
		$id=$_REQUEST['id'];
		$color=$_REQUEST['color'];
					
		$this->worksheet_model->add_work_color($id,$color);
		redirect('founder/worksheet/all_worksheet','refresh');
	}
	
	function delete_worksheet($id,$year_id,$month_id)
	{
		$this->worksheet_model->delete_worksheet($id);
		redirect('founder/worksheet/all_worksheet','refresh');
	}
	public function get_worksheet($id){
		$data['worksheet'] = $this->worksheet_model->get_details($id);
		$this->load->view('founder/edit_worksheet',$data);
	}
	public function update_worksheet(){
		$input = $this->input->post();
		$date_id = $input['date'];
		$month_id = $input['month'];	
		$year_id = $input['year'];
		$desc = $input['desc'];
		$user_id = $input['user_id'];
		if($date_id==date('d') && $month_id==date('m') && $year_id==date('y'))
		{
			$edited_flag = 0;
		}else{
			$edited_flag = 1;
		}
		
		$update = array(
		'desc'=> $desc,
		'edited_flag'=>$edited_flag,
		);		
		$this->worksheet_model->update($update,$input['id']);
	redirect('founder/worksheet/all_worksheet','refresh');
	}
	
	function verify_worksheet(){
			
			$user = $this->input->post('user_id');	
			$date = $this->input->post('date');
			$month = $this->input->post('month');
			$year = $this->input->post('year');
			$time = $this->input->post('time');
			$dbtime_tot = 0;
			
			
		
			$combine_worksheet = array();
			for($x=0;$x<count($user);$x++)
			{
				$dbtime_tot = 0;
				$w_where = array(
				'year_id' 	=> $year[$x],
				'month_id'	=> $month[$x],
				'user_id'	=> $user[$x],
				'date_id'	=> $date[$x]
				);
				
			$works = $this->worksheet_model->get_all_worksheet_by_date($w_where);
			
			foreach($works as $work){			
					$dbtime_explode = explode(':',$work->time_spent);
		 			$dbtot_hr 		= $dbtime_explode[0]*60*60;
					$dbtot_min		= $dbtime_explode[1]*60;				
					$dbtime_tot 	+= $dbtot_hr + $dbtot_min; 
					} 
				
				// breaking into hours and mins
						$hours 			= floor($dbtime_tot/ 3600);
						$minutes 		= floor(($dbtime_tot / 60) % 60);
						$sumtime[$x] 	= $hours .':'.$minutes.':00';
				
				$combine_worksheet[$x]['user_id'] = $user[$x];
				$combine_worksheet[$x]['date_id'] = $date[$x];
				$combine_worksheet[$x]['month_id'] = $month[$x];
				$combine_worksheet[$x]['year_id'] = $year[$x];
				$combine_worksheet[$x]['total_worktime'] = $sumtime[$x];
				$combine_worksheet[$x]['verify'] = 1;
			}
														
			$this->worksheet_model->worksheet_verified($combine_worksheet);
				
			redirect('/founder/worksheet/all_worksheet/'.$year[0].'/'.$month[0].'/'.$date[0],'refresh');
	}
	
	/**
	* Worksheet timings
	* @author : <pratik@fedobe.org>
	*/
	
	public function settings(){
		$meta['page_title'] = "Worksheet Timings";
		
		$data['settings'] = $this->worksheet_model->get_settings('worksheet');
		
		$this->load->view('common/header', $meta);
		$this->load->view('founder/worksheet_settings',$data);
		$this->load->view('common/footer');	
	}
	public function save_settings(){
		$input = $this->input->post();		
		$this->worksheet_model->save_settings($input);
		redirect('founder/worksheet/settings');
		}

}

?>